package org.apache.jmeter.extractor.gui;

import java.awt.BorderLayout;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.apache.jmeter.gui.util.HorizontalPanel;

import org.apache.jmeter.processor.gui.AbstractPostProcessorGui;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.extractor.JSONExtractor;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;

public class JSONExtractorGui extends AbstractPostProcessorGui {
	
	private static final long serialVersionUID = 240L;

	private static final Logger log = LoggingManager.getLoggerForClass();
	
	private JTextField refName = null;
	private JTextField jsonPath = null;
    private JTextField defaultValue = null;
    
    public JSONExtractorGui(){
    	super();
    	init();
    }
    
    private void init(){
    	setLayout(new BorderLayout());
        setBorder(makeBorder());

        add(makeTitlePanel(), BorderLayout.NORTH);
        
        JPanel mainPanel = new JPanel(new BorderLayout());
        HorizontalPanel subPanel = new HorizontalPanel();
        subPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Settings"));
        
        //HorizontalPanel Panel1 = new HorizontalPanel();
        refName = new JTextField(1);
        //refName.setText("");
        refName.setName("refName");
        subPanel.add(new JLabel("Reference Name:"));
        subPanel.add(refName);
        
        jsonPath = new JTextField(1);
        jsonPath.setText("$.");
        jsonPath.setName("JsonPathField");
        subPanel.add(new JLabel("JSON Path:"));
        subPanel.add(jsonPath);
        
                
        defaultValue = new JTextField(1);
        defaultValue.setName("defaultValue");
        subPanel.add(new JLabel("Default Value:"));
        subPanel.add(defaultValue);
        
        mainPanel.add(subPanel,BorderLayout.NORTH);
        
        add(mainPanel,BorderLayout.CENTER);
        
    }
    
    @Override
    public void configure(TestElement element){
    	super.configure(element);
    	JSONExtractor el = (JSONExtractor)element;
    	jsonPath.setText(el.getJsonpath());
    	refName.setText(el.getRefname());
    	defaultValue.setText(el.getDefault());
    }
    
    @Override
    public void clearGui(){
    	super.clearGui();
    	jsonPath.setText("$.");
    	refName.setText("");
    	defaultValue.setText("");
    }

	@Override
	public String getLabelResource() {
		// TODO Auto-generated method stub
		return "json_extractor_title";
	}
	
	@Override
	public String getStaticLabel(){
		return "JSON Extractor";
	}

	@Override
	public TestElement createTestElement() {
		// TODO Auto-generated method stub
		JSONExtractor element = new JSONExtractor();
		modifyTestElement(element);
		return element;
	}

	@Override
	public void modifyTestElement(TestElement element) {
		// TODO Auto-generated method stub
		log.debug("JSONAssertion.modifyTestElement() called");
		super.configureTestElement(element);
		if (element instanceof JSONExtractor){
			JSONExtractor el = (JSONExtractor)element;
			el.setJsonpath(jsonPath.getText());
			el.setRefname(refName.getText());
			el.setDefault(defaultValue.getText());
		}
	}

}
